//
//  ApageControl.m
//  DBAChain
//
//  Created by Mxionlly on 2018/9/7.
//  Copyright © 2018年 Evan. All rights reserved.
//

#import "ApageControl.h"

@implementation ApageControl


- (instancetype)init{
    self = [super init];
    if (self) {
        self.userInteractionEnabled = NO;
    }
    return self;
}

- (void)setCurrentPage:(NSInteger)currentPage{
    [super setCurrentPage:currentPage];
    for (int i = 0; i < [self.subviews count]; i++) {
        UIImageView *dot = [self imageViewForSubview:[self.subviews objectAtIndex:i] currPage:i];
        dot.layer.masksToBounds = YES;
        dot.layer.borderWidth = 1.0;
        dot.layer.cornerRadius = 3;
        if (i == self.currentPage){
            dot.backgroundColor = [UIColor whiteColor];
            dot.frame = CGRectMake(-6/(i== 0 ? 1 : 2) ,CGRectGetMinY(dot.frame),self.currentImageSizeW, self.currentImageSizeH);
            dot.layer.borderColor = [UIColor whiteColor].CGColor;
        }else{
            CGFloat interval = i !=0 ? self.currentImageSizeW/2.2 : 0;
            dot.backgroundColor = [UIColor colorWithWhite:1 alpha:0.3];
            dot.frame = CGRectMake(interval,CGRectGetMinY(dot.frame),self.currentImageSizeH, self.currentImageSizeH);
            dot.layer.borderColor = [UIColor colorWithWhite:1 alpha:0.5].CGColor;
        }
    }
}

- (UIImageView *)imageViewForSubview:(UIView *)view currPage:(int)currPage{
    UIImageView *dot = nil;
    if ([view isKindOfClass:[UIView class]]) {
        for (UIView *subview in view.subviews) {
            if ([subview isKindOfClass:[UIImageView class]]) {
                dot = (UIImageView *)subview;
                break;
            }
        }
        
        if (dot == nil) {
            dot = [[UIImageView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, view.frame.size.width, view.frame.size.height)];
            
            [view addSubview:dot];
        }
    }else {
        dot = (UIImageView *)view;
    }
    
    return dot;
}

@end
